/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author manolo
 */
/* Probando a partir de lo de jayway sigueindo los pasos */

//package proyectoparapruebas;

import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.*;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class Prueba1 {

    private String address;

    private Snmp snmp;

    public static void main(String args[]){
        Prueba1 client = new Prueba1("udp:127.0.0.1/161");
        String sysDescr;
        try {
            sysDescr = client.getResposnseAsString(new OID("1.3.6.1.2.1.1.1.0"));
            client.snmpSet(new OID("1.3.6.1.2.1.1.1.0"));
            //sysDescr = client.getAsString(new OID("'sysDescr.0'")); //asi no
            //me funciona, supongo que da igual
            System.out.println(sysDescr);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    public Prueba1(String address) {
	super();
	this.address = address;
	try {
            start();
	} catch (IOException e) {
            throw new RuntimeException(e);
	}
    }

    private void start() throws IOException {
        TransportMapping transport = new DefaultUdpTransportMapping();
	snmp = new Snmp(transport);
	transport.listen();
    }

    public String getResposnseAsString(OID oid) throws IOException {
	ResponseEvent event = get(new OID[]{oid});
	return event.getResponse().get(0).getVariable().toString();
    }

    public ResponseEvent get(OID oids[]) throws IOException {
	PDU pdu = new PDU();
 	for (OID oid : oids) {
 	     pdu.add(new VariableBinding(oid));
 	}
 	pdu.setType(PDU.GET);
 	ResponseEvent event = snmp.send(pdu, getTarget(), null);
	if(event != null) {
             return event;
	}
	throw new RuntimeException("GET timed out");
    }
    private Target getTarget() {
	Address targetAddress = GenericAddress.parse(address);
	CommunityTarget target = new CommunityTarget();
	target.setCommunity(new OctetString("public"));
	target.setAddress(targetAddress);
	target.setRetries(2);
	target.setTimeout(1500);
	target.setVersion(SnmpConstants.version2c);
	return target;
}

    public void snmpSet(OID strOID)
    {
        String strIPAddress = "127.0.0.1";
        System.out.println("Setting value corresponding to OID " + strOID);   


    }
    public void getSNMP(OID strOID)
    {



    }

}